import { isAuth } from '@/utils'
import { message } from 'antd'
import { Redirect, Route, RouteProps } from 'react-router-dom'

// ...rest获取剩余的参数
function PrivateRoute({ children, ...rest }: RouteProps) {
  // function PrivateRoute({ ...rest }: RouteProps) {
  // console.log('rest:', rest)
  return (
    <Route
      // 把其他的参数 原封不动的合并到 Route上 保证Route的功能 可以正常解析
      {...rest}
      render={({ location }) => {
        if (isAuth()) {
          return children
        } else {
          message.error('请先登录!!🐽🐽')
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: { from: location },
              }}
            />
          )
        }
      }}
    />
  )
}

export default PrivateRoute
